Programming - introducing 'While' Loops

OPENING QUESTIONS: 'For' loops are particularly helpful when we have to move (iterate!) through a loop a fixed number of times.

Work with your team to suggest a situation where you won't know the number of times you'll be iterating (that's one of our words of the day: IT-er-a-ting) through a loop

LEARNING TARGET: I will rewrite my leap year program using a 'while' loop during today's class

COURSE DOCUMENT: Ver 1.70 is HERE

WORDS O' THE DAY:

For Loop (Used to move through a loop a certain number of times and keeping track of each time through the loop using a counter variable that is incremented by a specific value each time

While Loop (Used to move through a loop as long as the initial condition is TRUE)

Iterate (To move through a loop, each time through the loop is called one 'iteration')

WORK O' THE DAY

Let's begin with a wee bit of show-and-tell and see how our speed loops compare. Be sure and talk with your team and edit in a timer that records the time at the start of the loop and at the end of the loop and calculates the total run time:

Something like this (feel free to use this code if you like):

JS Functions work:

var startTime = Date.now();

var endTime = Date.now();

var elapsedTime = endTime - startTime;

So do App Lab functions:

var startTime = getTime();

var endTime = getTime();

var elapsedTime = endTime - startTime;

Note that the result will be in milliseconds. DO a wee bit of math so that the result output to the console log is in seconds with decimals

 

Now let's back to the wonderful world of while loops.

Please re-write your leap year program using a while loop